package com.my.easymall.service;


import com.my.easymall.dao.UserDao;
import com.my.easymall.domain.User;
import com.my.easymall.exception.EmailExistException;
import com.my.easymall.exception.MsgException;
import com.my.easymall.exception.UsernameExistException;
import org.junit.Test;

/**
 * @auther Summerday
 *
 * 处理业务逻辑
 */
public class UserService{

    private UserDao dao = new UserDao();
    /**
     *
     * @param user
     */
    public void registUser(User user) {
        //判断用户名是否存在
        if(UserEmailExist(user)){
           throw new UsernameExistException("用户名已存在");
            //判断邮箱是否存在
        }else if(UsernameExist(user)){
            throw new EmailExistException("邮箱已存在");
        }

        dao.addUser(user);
    }

    /**
     * 根据User对象实现登录功能,并将用户信息返回
     * @param user
     * @return 返回用户信息User,如果没有查询到,返回null
     */

    public User loginUser(User user) {
        User loginUser = dao.loginByUsernameAndPassword(user);
        if(loginUser!=null){
            return loginUser;
        }else{
            throw new MsgException("用户名或密码不正确");
        }

    }


    public boolean UsernameExist(User user) {
        return dao.findByUsername(user)!=null;
    }


    public boolean UserEmailExist(User user) {
        return dao.findByUserEmail(user)!=null;
    }

    @Test
    public void test(){
        User user = new User();
        user.setUsername("addmin");
        boolean b = UsernameExist(user);
        System.out.println(b);
    }
}
